package com.bookshop.service;

import com.bookshop.dao.ICartDao;
import com.bookshop.model.OrderItemModel;
import com.bookshop.model.OrdersModel;
import com.bookshop.vo.CartVo;
import com.bookshop.vo.OrderVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartService {
    @Autowired
    private ICartDao dao;
    public List<CartVo> findCartById(int userId){
        return dao.findCartById(userId);
    }
    @Transactional(rollbackFor = Exception.class)
    public int addOrder(OrdersModel model, OrderItemModel orderItemModel){
        Logger logger = Logger.getLogger(CartService.class);
        dao.addOrders(model);
        logger.info("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"+model.getOrderId());
        orderItemModel.setOrderId(model.getOrderId());
        int j =dao.addOrderItem(orderItemModel);
        return j;
    }

    public void deleteOrder(String orderId) {
        dao.deleteOrder(orderId);
    }

    public void changeQuantity(String orderId, String quantity) {
        Map param=new HashMap();
        param.put("orderId",orderId);
        param.put("quantity",quantity);
        dao.changeQuantity(param);
    }

    public List<OrderVo> getCartList() {
        return dao.getCartList();
    }
}
